1 =============================================================================
2 CONSOLE APPLICATION : CSPlatformDetector Project Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 The CSPlatformDetector code sample demonstrates the following tasks related
11 1. Detect the name of the current operating system.
12 (e.g. "Microsoft Windows 7 Enterprise")
13 2. Detect the version of the current operating system.
14 (e.g. "Microsoft Windows NT 6.1.7600.0")
15 3. Determine whether the current operating system is a 64-bit operating
17 4. Determine whether the current process is a 64-bit process.
18 5. Determine whether an arbitrary process running on the system is 64-bit.
21 /////////////////////////////////////////////////////////////////////////////
24 The following steps walk through a demonstration of the sample.
26 Step1. After you successfully build the sample project in Visual Studio 2010
27 targeting the "Any CPU" platform, you will get an application:
28 CSPlatformDetector.exe.
30 Step2. Run the application in a command prompt (cmd.exe) on a 64-bit
31 operating system (e.g. Windows 7 x64 Ultimate). The application prints the
32 following information in the command prompt:
34 Current OS: Microsoft Windows 7 Ultimate
35 Version: Microsoft Windows NT 6.1.7600.0
37 Current process is 64-bit
39 It dictates that the current operating system is Microsoft Windows 7 Ultimate.
40 Its version is 6.1.7600.0. The OS is a workstation instead of a server or
41 domain controller. The system is 64-bit. The current process is a 64-bit
44 Step3. In Task Manager, find a 32-bit process running on the system, and get
45 its process ID (e.g. 6100). Run CSPlatformDetector.exe with the process ID
46 as the first argument. For example,
48 CSPlatformDetector.exe 6100
50 The application will output:
53 Process 6100 is not 64-bit
55 It indicates that the specified process is not a 64-bit process.
58 /////////////////////////////////////////////////////////////////////////////
61 A. Get the name of the current operating system.
62 (e.g. "Microsoft Windows 7 Enterprise")
64 The name of the operating system (e.g. "Microsoft Windows 7 Ultimate") can be
65 retrieved from the Caption property of the Win32_OperatingSystem WMI class
66 (http://msdn.microsoft.com/en-us/library/aa394239.aspx). You can find the VC#
67 code that queries the value of Win32_OperatingSystem.Caption in the GetOSName
70 Alternatively, you can build the string of the operating system name by using
71 the GetVersionEx, GetSystemMetrics, GetProductInfo, and GetNativeSystemInfo
72 functions. The MSDN article "Getting the System Version" gives an C++ example:
73 http://msdn.microsoft.com/en-us/library/ms724429.aspx. However, the solution
74 is not flexible for new releases of operating systems.
78 B. Get the version of the current operating system.
79 (e.g. "Microsoft Windows NT 6.1.7600.0")
81 The System.Environment.OSVersion property returns an OperatingSystem object
82 that contains the current platform identifier and version numbers.
83 http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx
84 http://msdn.microsoft.com/en-us/library/system.operatingsystem.aspx
85 You can use these numbers to quickly determine what the operating system is,
86 whether a certain Service Pack is installed, etc.
88 In the code sample, Environment.OSVersion.VersionString gets the concatenated
89 string representation of the platform identifier, version, and service pack
90 that are currently installed on the operating system. For example,
91 "Microsoft Windows NT 6.1.7600.0".
95 C. Determine the whether the current OS is a 64-bit operating system.
97 The Environment.Is64BitOperatingSystem property new in .NET Framework 4
98 determines whether the current operating system is a 64-bit operating system.
99 http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx
101 The implementation of Environment.Is64BitOperatingSystem is based on this
104 If the running process is a 64-bit process, the operating system must be a
105 64-bit operating system.
107 If the running process is a 32-bit process, the process may be running in a
108 32-bit operating system, or under WOW64 of a 64-bit operating system. To
109 detect whether the 32-bit program is running in a 64-bit operating system,
110 you can use the IsWow64Process function.
113 return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process")
114 && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag))
119 D. Determine whether the current process or an arbitrary process running on
120 the system is a 64-bit process.
122 If you are determining whether the currently running process is a 64-bit
123 process, you can use the Environment.Is64BitProcess property new in .NET
125 http://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx
127 If you are detecting whether an arbitrary application running on the system
128 is a 64-bit process, you need to determine the OS bitness and if it is 64-bit,
129 call IsWow64Process() with the target process handle.
131 static bool Is64BitProcess(IntPtr hProcess)
135 if (Environment.Is64BitOperatingSystem)
137 // On 64-bit OS, if a process is not running under Wow64 mode,
138 // the process must be a 64-bit process.
139 flag = !(NativeMethods.IsWow64Process(hProcess, out flag) && flag);
146 /////////////////////////////////////////////////////////////////////////////
149 MSDN: Environment.Is64BitOperatingSystem Property
150 http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx
152 MSDN: Environment.Is64BitProcess Property
153 http://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx
155 MSDN: Getting the System Version
156 http://msdn.microsoft.com/en-us/library/ms724429.aspx
158 How to detect programmatically whether you are running on 64-bit Windows
159 http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx
162 /////////////////////////////////////////////////////////////////////////////